package com.jn.rxjava.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.rx_java_demo.R;
import com.qweather.sdk.bean.weather.WeatherDailyBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DailyWeatherAdapter extends RecyclerView.Adapter<DailyWeatherAdapter.DailyViewHolder> {

    private List<WeatherDailyBean.DailyBean> dailyDataList;
    private Context context;

    public DailyWeatherAdapter(List<WeatherDailyBean.DailyBean> dailyDataList, Context context) {
        this.dailyDataList = dailyDataList;
        this.context = context;
    }

    @NonNull
    @Override
    public DailyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_weather_future_item, parent, false);
        return new DailyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull DailyViewHolder holder, int position) {
        WeatherDailyBean.DailyBean dailyData = dailyDataList.get(position);

        // 绑定数据到视图
        holder.timeTextView.setText(dailyData.getFxDate().substring(5, 10)); // 显示日期，例如 "10/21"

        // 显示当天是周几
        String dayOfWeek = getDayOfWeek(dailyData.getFxDate());
        holder.weatherTextView.setText(dayOfWeek);

        if (holder.iconImageView != null) { // 添加空检查
            holder.iconImageView.setImageResource(getIconResource(dailyData.getIconDay())); // 设置天气图标
        } else {
            Log.e("Weather", "iconImageView is null");
        }

        holder.windTextView.setText(dailyData.getTempMin() + "°/" + dailyData.getTempMax() + "°"); // 显示温度范围
    }

    @Override
    public int getItemCount() {
        return dailyDataList.size();
    }

    private int getIconResource(String iconId) {
        // 这里应该根据iconId返回对应的资源ID
        // 示例映射表
        switch (iconId) {
            case "100":
                return R.drawable.ic_clear_day;
            case "101":
                return R.drawable.tiqnqi_qingtianzhuanyun;
            // 添加更多映射
            case "102":
                return R.drawable.ic_few_clouds;
            case "104":
                return R.drawable.ic_cloudy_weather;
            case "302":
                return R.drawable.ic_storm_weather;
            case "399":
                return R.drawable.rain;
            case "901":
                return R.drawable.ic_mostly_cloudy;
            default:
                return R.drawable.ic_unknown;
        }
    }

    private String getDayOfWeek(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            switch (dayOfWeek) {
                case Calendar.SUNDAY:
                    return "星期日";
                case Calendar.MONDAY:
                    return "星期一";
                case Calendar.TUESDAY:
                    return "星期二";
                case Calendar.WEDNESDAY:
                    return "星期三";
                case Calendar.THURSDAY:
                    return "星期四";
                case Calendar.FRIDAY:
                    return "星期五";
                case Calendar.SATURDAY:
                    return "星期六";
                default:
                    return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    static class DailyViewHolder extends RecyclerView.ViewHolder {
        TextView timeTextView;
        TextView weatherTextView;
        ImageView iconImageView;
        TextView windTextView;

        DailyViewHolder(View itemView) {
            super(itemView);
            timeTextView = itemView.findViewById(R.id.wk_time);
            weatherTextView = itemView.findViewById(R.id.wk_weather);
            iconImageView = itemView.findViewById(R.id.future_weather_item);
            windTextView = itemView.findViewById(R.id.wk_wind);
        }
    }
}